class Solution {
public:
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int row, col, sign;
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if (image[sr][sc] == color) return image;
        row = image.size();
        col = image[0].size();
        sign = image[sr][sc];
        dfs(image, sr, sc, color);
        return image;
    }

    void dfs(vector<vector<int>>& image, int sr, int sc, int color)
    {
        image[sr][sc] = color;
        for (int i = 0; i < 4; i++)
        {
            int x = sr + dx[i];
            int y = sc + dy[i];
            if (x >= 0 && x < row && y >= 0 && y < col && image[x][y] == sign)
                dfs(image, x, y, color);
        }
        return;
    }
};
class Solution {
public:

    bool flag[300][300];
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int row, col, ret;
    int numIslands(vector<vector<char>>& image) {
        row = image.size();
        col = image[0].size();
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                if (image[i][j] == '1' && flag[i][j] == false)
                {
                    flag[i][j] = true;
                    dfs(image, i, j);
                    ret++;
                }
        return ret;
    }

    void dfs(vector<vector<char>>& image, int sr, int sc)
    {
        for (int i = 0; i < 4; i++)
        {
            int x = sr + dx[i];
            int y = sc + dy[i];
            if (x >= 0 && x < row && y >= 0 && y < col && image[x][y] == '1' && flag[x][y] == false)
            {
                flag[x][y] = true;
                dfs(image, x, y);
            }
        }
    }
};
class Solution {
public:
    bool flag[50][50];
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int row, col, ret, count;
    int maxAreaOfIsland(vector<vector<int>>& image) {
        ret = 0;
        row = image.size();
        col = image[0].size();
        for (int i = 0; i < row; i++)
            for (int j = 0; j < col; j++)
                if (image[i][j] == 1 && flag[i][j] == false)
                {
                    count = 1;
                    flag[i][j] = true;
                    dfs(image, i, j);
                    ret = max(ret, count);
                }
        return ret;
    }

    void dfs(vector<vector<int>>& image, int sr, int sc)
    {
        for (int i = 0; i < 4; i++)
        {
            int x = sr + dx[i];
            int y = sc + dy[i];
            if (x >= 0 && x < row && y >= 0 && y < col && image[x][y] == 1 && flag[x][y] == false)
            {
                count++;
                flag[x][y] = true;
                dfs(image, x, y);
            }
        }
    }
};
